package com.xiaotou.domain.impl;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import com.xiaotou.db.pojo.Item;
import com.xiaotou.domain.AvailableItem;
import com.xiaotou.domain.Engine;
import com.xiaotou.domain.Prey;
import com.xiaotou.domain.RuleParam;
import com.xiaotou.domain.WealthLevel;
import com.xiaotou.weibo.WeiboUser;

public class PreyImpl implements Prey {

	private Engine engine;

	private WeiboUser actor;
	private RuleParam ruleParam;

	public PreyImpl(WeiboUser actor) {
		super();
		this.actor = actor;
	}

	@Override
	public WeiboUser getActor() {
		return this.actor;
	}

	@Override
	public WealthLevel getWealthLevel() {
		return engine.getWealthLevel(this);
	}

	@Override
	public int getAvailableMoney() {
		return actor.getFriendsCount() * ruleParam.getMoneyWeights();
	}

	@Override
	public Set<AvailableItem> getAvailableItems() {
		Collection<Item> allItems = getAllItems();
		Set<AvailableItem> availableItems = new HashSet<AvailableItem>();
		for (Item item : allItems) {
			final AvailableItemImpl availableItem = new AvailableItemImpl();
			availableItem.setName(item.getName());
			availableItem.setAmount(engine.getItemAmount(this, item.getName()));
			availableItems.add(availableItem);
		}
		return availableItems;
	}

	private Collection<Item> getAllItems() {
		return this.engine.getAllItems();
	}

	public void setRuleParam(RuleParam ruleParam) {
		this.ruleParam = ruleParam;
	}

	public void setEngine(Engine engine) {
		this.engine = engine;
	}
}
